package com.itheima.service.impl;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {


    //本地注入
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量导入预约设置
    @Override
    public void add(List<OrderSetting> list) {
         //先进性判断
        if (list !=null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate>0){
                   //已经创建过了 执行修改方法
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //未创建过 执行创建方法
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份查看人数
    @Override
    public List<Map> getOrderSettingByMonth(String data) {
        String begin = data + "-1";
        String end = data + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);

        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            //遍历 list 去除 orderSetting 对象
            for (OrderSetting orderSetting : list) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("date", orderSetting.getOrderDate().getDate());
                map1.put("number", orderSetting.getNumber());
                map1.put("reservations", orderSetting.getReservations());
                result.add(map1);
            }
        }
        return result;
    }

    //根据日期设置预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //先进行判断 如果当天该数据没有设置 为增加  如果有数据 为更改
        Date orderDate = orderSetting.getOrderDate();
        //判断当前日期是否已经进行了预约设置
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
        if(countByOrderDate>0){
            //已进行了预约设置
           orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //还未进行设置
            orderSettingDao.add(orderSetting);
        }
    }

}
